package cn.eonshine.cuba.inf.modular.logs.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;

import cn.eonshine.base.core.base.controller.BaseController;
import cn.eonshine.base.core.common.constant.factory.PageFactory;
import cn.eonshine.cuba.inf.common.http.InfStatus;
import cn.eonshine.cuba.inf.modular.log.entity.Log;
import cn.eonshine.cuba.inf.modular.log.service.ILogService;
import cn.eonshine.cuba.inf.modular.sys.entity.Dict;
import cn.eonshine.cuba.inf.modular.sys.service.IDictService;

/**
 * 控制器
 *
 * @author fengshuonan
 * @Date 2018-11-06 09:28:44
 */
@Controller
@RequestMapping(value={"/infLog","/rest/infLog"})
public class InfLogController extends BaseController {

    private String PREFIX = "/infLog/";

    @Autowired
    private IDictService dictService;

    @Autowired
    private ILogService infLogService;

    @RequestMapping("")
    public String index() {
        return PREFIX + "infLog.html";
    }

    @RequestMapping("/infLog_add")
    public String infLogAdd() {
        return PREFIX + "infLog_add.html";
    }

    @RequestMapping("/infLog_update/{infLogId}")
    public String infLogUpdate(@PathVariable String infLogId, Model model) {
        Log infLog = infLogService.getById(infLogId);
        model.addAttribute("item",infLog);
//        LogObjectHolder.me().set(infLog);
        return PREFIX + "infLog_edit.html";
    }

    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition, String logTypeId) {
    	IPage<Log> page=new PageFactory<Log>().defaultPage();
    	page=infLogService.searchList(page,condition, logTypeId);
    	return packForBT((Page<Log>) page);
    }

    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Log infLog) {
        infLogService.save(infLog);
        return SUCCESS_TIP;
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam String infLogId) {
    	
        infLogService.deleteByOne(infLogId);
        return SUCCESS_TIP;
    }

    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Log infLog) {
        infLogService.updateById(infLog);
        return SUCCESS_TIP;
    }

    @RequestMapping(value = "/detail/{infLogId}")
    public String detail(@PathVariable("infLogId") String infLogId, Model model) {
        Log infLog= viewDetail(infLogId);
        model.addAttribute("infLog",infLog);
        return PREFIX + "infLog_edit.html";
    }

    @RequestMapping(value = "/detailView/{infLogId}")
    @ResponseBody
    public Log viewDetail(@PathVariable("infLogId") String infLogId) {
    	Log infLog= infLogService.selectDetail(infLogId);
    	return infLog;
    }
    
    @RequestMapping(value = "/getLogTypeDicts")
    @ResponseBody
    public List<Dict> getLogTypeDicts(){
    	Dict dataType = dictService.getOne(new QueryWrapper<Dict>().lambda().eq(Dict::getId, InfStatus.INF_LOG_TYPE.DICT_DATA_TYPE_ID));
    	List<Dict> subDicts = Lists.newArrayList();
    	if(dataType != null) {
    		QueryWrapper<Dict> query = new QueryWrapper<Dict>();
    		query.lambda().eq(Dict::getPid, dataType.getId()).orderByAsc(Dict::getNum);
    		subDicts = dictService.list(query);
    	}
    	return subDicts;
    }
}
